home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / perl / dosperl.zoo / perldb.pl < prev    next >
Perl Script  |  1991-04-13  |  15KB  |  545 lines

  1. package DB;
  2.  
  3. $header = '$Header: perldb.pl,v 4.0 91/03/20 01:25:50 lwall Locked $';
  4. #
  5. # This file is automatically included if you do perl -d.
  6. # It's probably not useful to include this yourself.
  7. #
  8. # Perl supplies the values for @line and %sub.  It effectively inserts
  9. # a do DB'DB(<linenum>); in front of every place that can
  10. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  11. #
  12. # $Log:    perldb.pl,v $
  13. # Revision 4.0  91/03/20  01:25:50  lwall
  14. # 4.0 baseline.
  15. # Revision 3.0.1.6  91/01/11  18:08:58  lwall
  16. # patch42: @_ couldn't be accessed from debugger
  17. # Revision 3.0.1.5  90/11/10  01:40:26  lwall
  18. # patch38: the debugger wouldn't stop correctly or do action routines
  19. # Revision 3.0.1.4  90/10/15  17:40:38  lwall
  20. # patch29: added caller
  21. # patch29: the debugger now understands packages and evals
  22. # patch29: scripts now run at almost full speed under the debugger
  23. # patch29: more variables are settable from debugger
  24. # Revision 3.0.1.3  90/08/09  04:00:58  lwall
  25. # patch19: debugger now allows continuation lines
  26. # patch19: debugger can now dump lists of variables
  27. # patch19: debugger can now add aliases easily from prompt
  28. # Revision 3.0.1.2  90/03/12  16:39:39  lwall
  29. # patch13: perl -d didn't format stack traces of *foo right
  30. # patch13: perl -d wiped out scalar return values of subroutines
  31. # Revision 3.0.1.1  89/10/26  23:14:02  lwall
  32. # patch1: RCS expanded an unintended $Header in lib/perldb.pl
  33. # Revision 3.0  89/10/18  15:19:46  lwall
  34. # 3.0 baseline
  35. # Revision 2.0  88/06/05  00:09:45  root
  36. # Baseline version 2.0.
  37. #
  38.  
  39. open(IN, "</dev/tty") || open(IN,  "<&STDIN");    # so we don't dingle stdin
  40. open(OUT,">/dev/tty") || open(OUT, ">&STDOUT");    # so we don't dongle stdout
  41. select(OUT);
  42. $| = 1;                # for DB'OUT
  43. select(STDOUT);
  44. $| = 1;                # for real STDOUT
  45. $sub = '';
  46.  
  47. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  48. print OUT "\nLoading DB routines from $header\n\nEnter h for help.\n\n";
  49.  
  50. sub DB {
  51.     &save;
  52.     ($package, $filename, $line) = caller;
  53.     $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
  54.     "package $package;";        # this won't let them modify, alas
  55.     local(*dbline) = "_<$filename";
  56.     $max = $#dbline;
  57.     if (($stop,$action) = split(/\0/,$dbline{$line})) {
  58.     if ($stop eq '1') {
  59.         $signal |= 1;
  60.     }
  61.     else {
  62.         $evalarg = "\$DB'signal |= do {$stop;}"; &eval;
  63.         $dbline{$line} =~ s/;9($|\0)/$1/;
  64.     }
  65.     }
  66.     if ($single || $trace || $signal) {
  67.     print OUT "$package'" unless $sub =~ /'/;
  68.     print OUT "$sub($filename:$line):\t",$dbline[$line];
  69.     for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  70.         last if $dbline[$i] =~ /^\s*(}|#|\n)/;
  71.         print OUT "$sub($filename:$i):\t",$dbline[$i];
  72.     }
  73.     }
  74.     $evalarg = $action, &eval if $action;
  75.     if ($single || $signal) {
  76.     $evalarg = $pre, &eval if $pre;
  77.     print OUT $#stack . " levels deep in subroutine calls!\n"
  78.         if $single & 4;
  79.     $start = $line;
  80.       CMD:
  81.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  82.         {
  83.         $single = 0;
  84.         $signal = 0;
  85.         $cmd eq '' && exit 0;
  86.         chop($cmd);
  87.         $cmd =~ s/\\$// && do {
  88.             print OUT "  cont: ";
  89.             $cmd .= &gets;
  90.             redo CMD;
  91.         };
  92.         $cmd =~ /^q$/ && exit 0;
  93.         $cmd =~ /^$/ && ($cmd = $laststep);
  94.         push(@hist,$cmd) if length($cmd) > 1;
  95.         ($i) = split(/\s+/,$cmd);
  96.         eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  97.         $cmd =~ /^h$/ && do {
  98.             print OUT "
  99. T        Stack trace.
  100. s        Single step.
  101. n        Next, steps over subroutine calls.
  102. r        Return from current subroutine.
  103. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  104.         at the specified line.
  105. <CR>        Repeat last n or s.
  106. l min+incr    List incr+1 lines starting at min.
  107. l min-max    List lines.
  108. l line        List line;
  109. l        List next window.
  110. -        List previous window.
  111. w line        List window around line.
  112. l subname    List subroutine.
  113. f filename    Switch to filename.
  114. /pattern/    Search forwards for pattern; final / is optional.
  115. ?pattern?    Search backwards for pattern.
  116. L        List breakpoints and actions.
  117. S        List subroutine names.
  118. t        Toggle trace mode.
  119. b [line] [condition]
  120.         Set breakpoint; line defaults to the current execution line; 
  121.         condition breaks if it evaluates to true, defaults to \'1\'.
  122. b subname [condition]
  123.         Set breakpoint at first line of subroutine.
  124. d [line]    Delete breakpoint.
  125. D        Delete all breakpoints.
  126. a [line] command
  127.         Set an action to be done before the line is executed.
  128.         Sequence is: check for breakpoint, print line if necessary,
  129.         do action, prompt user if breakpoint or step, evaluate line.
  130. A        Delete all actions.
  131. V [pkg [vars]]    List some (default all) variables in package (default current).
  132. X [vars]    Same as \"V currentpackage [vars]\".
  133. < command    Define command before prompt.
  134. > command    Define command after prompt.
  135. ! number    Redo command (default previous command).
  136. ! -number    Redo number\'th to last command.
  137. H -number    Display last number commands (default all).
  138. q or ^D        Quit.
  139. p expr        Same as \"print DB'OUT expr\" in current package.
  140. = [alias value]    Define a command alias, or list current aliases.
  141. command        Execute as a perl statement in current package.
  142.  
  143. ";
  144.             next CMD; };
  145.         $cmd =~ /^t$/ && do {
  146.             $trace = !$trace;
  147.             print OUT "Trace = ".($trace?"on":"off")."\n";
  148.             next CMD; };
  149.         $cmd =~ /^S$/ && do {
  150.             foreach $subname (sort(keys %sub)) {
  151.             print OUT $subname,"\n";
  152.             }
  153.             next CMD; };
  154.         $cmd =~ s/^X\b/V $package/;
  155.         $cmd =~ /^V$/ && do {
  156.             $cmd = 'V $package'; };
  157.         $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  158.             $packname = $1;
  159.             @vars = split(' ',$2);
  160.             do 'dumpvar.pl' unless defined &main'dumpvar;
  161.             if (defined &main'dumpvar) {
  162.             &main'dumpvar($packname,@vars);
  163.             }
  164.             else {
  165.             print DB'OUT "dumpvar.pl not available.\n";
  166.             }
  167.             next CMD; };
  168.         $cmd =~ /^f\b\s*(.*)/ && do {
  169.             $file = $1;
  170.             if (!$file) {
  171.             print OUT "The old f command is now the r command.\n";
  172.             print OUT "The new f command switches filenames.\n";
  173.             next CMD;
  174.             }
  175.             if (!defined $_main{'_<' . $file}) {
  176.             if (($try) = grep(m#^_<.*$file#, keys %_main)) {
  177.                 $file = substr($try,2);
  178.                 print "\n$file:\n";
  179.             }
  180.             }
  181.             if (!defined $_main{'_<' . $file}) {
  182.             print OUT "There's no code here anything matching $file.\n";
  183.             next CMD;
  184.             }
  185.             elsif ($file ne $filename) {
  186.             *dbline = "_<$file";
  187.             $max = $#dbline;
  188.             $filename = $file;
  189.             $start = 1;
  190.             $cmd = "l";
  191.             } };
  192.         $cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
  193.             $subname = $1;
  194.             $subname = "main'" . $subname unless $subname =~ /'/;
  195.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  196.             ($file,$subrange) = split(/:/,$sub{$subname});
  197.             if ($file ne $filename) {
  198.             *dbline = "_<$file";
  199.             $max = $#dbline;
  200.             $filename = $file;
  201.             }
  202.             if ($subrange) {
  203.             if (eval($subrange) < -$window) {
  204.                 $subrange =~ s/-.*/+/;
  205.             }
  206.             $cmd = "l $subrange";
  207.             } else {
  208.             print OUT "Subroutine $1 not found.\n";
  209.             next CMD;
  210.             } };
  211.         $cmd =~ /^w\b\s*(\d*)$/ && do {
  212.             $incr = $window - 1;
  213.             $start = $1 if $1;
  214.             $start -= $preview;
  215.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  216.         $cmd =~ /^-$/ && do {
  217.             $incr = $window - 1;
  218.             $cmd = 'l ' . ($start-$window*2) . '+'; };
  219.         $cmd =~ /^l$/ && do {
  220.             $incr = $window - 1;
  221.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  222.         $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  223.             $start = $1 if $1;
  224.             $incr = $2;
  225.             $incr = $window - 1 unless $incr;
  226.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  227.         $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  228.             $end = (!$2) ? $max : ($4 ? $4 : $2);
  229.             $end = $max if $end > $max;
  230.             $i = $2;
  231.             $i = $line if $i eq '.';
  232.             $i = 1 if $i < 1;
  233.             for (; $i <= $end; $i++) {
  234.             print OUT "$i:\t", $dbline[$i];
  235.             last if $signal;
  236.             }
  237.             $start = $i;    # remember in case they want more
  238.             $start = $max if $start > $max;
  239.             next CMD; };
  240.         $cmd =~ /^D$/ && do {
  241.             print OUT "Deleting all breakpoints...\n";
  242.             for ($i = 1; $i <= $max ; $i++) {
  243.             if (defined $dbline{$i}) {
  244.                 $dbline{$i} =~ s/^[^\0]+//;
  245.                 if ($dbline{$i} =~ s/^\0?$//) {
  246.                 delete $dbline{$i};
  247.                 }
  248.             }
  249.             }
  250.             next CMD; };
  251.         $cmd =~ /^L$/ && do {
  252.             for ($i = 1; $i <= $max; $i++) {
  253.             if (defined $dbline{$i}) {
  254.                 print OUT "$i:\t", $dbline[$i];
  255.                 ($stop,$action) = split(/\0/, $dbline{$i});
  256.                 print OUT "  break if (", $stop, ")\n" 
  257.                 if $stop;
  258.                 print OUT "  action:  ", $action, "\n" 
  259.                 if $action;
  260.                 last if $signal;
  261.             }
  262.             }
  263.             next CMD; };
  264.         $cmd =~ /^b\b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
  265.             $subname = $1;
  266.             $cond = $2 || '1';
  267.             $subname = "$package'" . $subname unless $subname =~ /'/;
  268.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  269.             ($filename,$i) = split(/[:-]/, $sub{$subname});
  270.             if ($i) {
  271.             *dbline = "_<$filename";
  272.             ++$i while $dbline[$i] == 0 && $i < $#dbline;
  273.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  274.             } else {
  275.             print OUT "Subroutine $subname not found.\n";
  276.             }
  277.             next CMD; };
  278.         $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
  279.             $i = ($1?$1:$line);
  280.             $cond = $2 || '1';
  281.             if ($dbline[$i] == 0) {
  282.             print OUT "Line $i not breakable.\n";
  283.             } else {
  284.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  285.             }
  286.             next CMD; };
  287.         $cmd =~ /^d\b\s*(\d+)?/ && do {
  288.             $i = ($1?$1:$line);
  289.             $dbline{$i} =~ s/^[^\0]*//;
  290.             delete $dbline{$i} if $dbline{$i} eq '';
  291.             next CMD; };
  292.         $cmd =~ /^A$/ && do {
  293.             for ($i = 1; $i <= $max ; $i++) {
  294.             if (defined $dbline{$i}) {
  295.                 $dbline{$i} =~ s/\0[^\0]*//;
  296.                 delete $dbline{$i} if $dbline{$i} eq '';
  297.             }
  298.             }
  299.             next CMD; };
  300.         $cmd =~ /^<\s*(.*)/ && do {
  301.             $pre = do action($1);
  302.             next CMD; };
  303.         $cmd =~ /^>\s*(.*)/ && do {
  304.             $post = do action($1);
  305.             next CMD; };
  306.         $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
  307.             $i = $1;
  308.             if ($dbline[$i] == 0) {
  309.             print OUT "Line $i may not have an action.\n";
  310.             } else {
  311.             $dbline{$i} =~ s/\0[^\0]*//;
  312.             $dbline{$i} .= "\0" . do action($3);
  313.             }
  314.             next CMD; };
  315.         $cmd =~ /^n$/ && do {
  316.             $single = 2;
  317.             $laststep = $cmd;
  318.             last CMD; };
  319.         $cmd =~ /^s$/ && do {
  320.             $single = 1;
  321.             $laststep = $cmd;
  322.             last CMD; };
  323.         $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
  324.             $i = $1;
  325.             if ($i) {
  326.             if ($dbline[$i] == 0) {
  327.                 print OUT "Line $i not breakable.\n";
  328.                 next CMD;
  329.             }
  330.             $dbline{$i} =~ s/(\0|$)/;9$1/;    # add one-time-only b.p.
  331.             }
  332.             for ($i=0; $i <= $#stack; ) {
  333.             $stack[$i++] &= ~1;
  334.             }
  335.             last CMD; };
  336.         $cmd =~ /^r$/ && do {
  337.             $stack[$#stack] |= 2;
  338.             last CMD; };
  339.         $cmd =~ /^T$/ && do {
  340.             local($p,$f,$l,$s,$h,$a,@a,@sub);
  341.             for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  342.             @a = @args;
  343.             for (@a) {
  344.                 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  345.                 $_ = sprintf("%s",$_);
  346.                 }
  347.                 else {
  348.                 s/'/\\'/g;
  349.                 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  350.                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  351.                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  352.                 }
  353.             }
  354.             $w = $w ? '@ = ' : '$ = ';
  355.             $a = $h ? '(' . join(', ', @a) . ')' : '';
  356.             push(@sub, "$w&$s$a from file $f line $l\n");
  357.             last if $signal;
  358.             }
  359.             for ($i=0; $i <= $#sub; $i++) {
  360.             last if $signal;
  361.             print OUT $sub[$i];
  362.             }
  363.             next CMD; };
  364.         $cmd =~ /^\/(.*)$/ && do {
  365.             $inpat = $1;
  366.             $inpat =~ s:([^\\])/$:$1:;
  367.             if ($inpat ne "") {
  368.             eval '$inpat =~ m'."\n$inpat\n";    
  369.             if ($@ ne "") {
  370.                 print OUT "$@";
  371.                 next CMD;
  372.             }
  373.             $pat = $inpat;
  374.             }
  375.             $end = $start;
  376.             eval '
  377.             for (;;) {
  378.             ++$start;
  379.             $start = 1 if ($start > $max);
  380.             last if ($start == $end);
  381.             if ($dbline[$start] =~ m'."\n$pat\n".'i) {
  382.                 print OUT "$start:\t", $dbline[$start], "\n";
  383.                 last;
  384.             }
  385.             } ';
  386.             print OUT "/$pat/: not found\n" if ($start == $end);
  387.             next CMD; };
  388.         $cmd =~ /^\?(.*)$/ && do {
  389.             $inpat = $1;
  390.             $inpat =~ s:([^\\])\?$:$1:;
  391.             if ($inpat ne "") {
  392.             eval '$inpat =~ m'."\n$inpat\n";    
  393.             if ($@ ne "") {
  394.                 print OUT "$@";
  395.                 next CMD;
  396.             }
  397.             $pat = $inpat;
  398.             }
  399.             $end = $start;
  400.             eval '
  401.             for (;;) {
  402.             --$start;
  403.             $start = $max if ($start <= 0);
  404.             last if ($start == $end);
  405.             if ($dbline[$start] =~ m'."\n$pat\n".'i) {
  406.                 print OUT "$start:\t", $dbline[$start], "\n";
  407.                 last;
  408.             }
  409.             } ';
  410.             print OUT "?$pat?: not found\n" if ($start == $end);
  411.             next CMD; };
  412.         $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
  413.             pop(@hist) if length($cmd) > 1;
  414.             $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
  415.             $cmd = $hist[$i] . "\n";
  416.             print OUT $cmd;
  417.             redo CMD; };
  418.         $cmd =~ /^!(.+)$/ && do {
  419.             $pat = "^$1";
  420.             pop(@hist) if length($cmd) > 1;
  421.             for ($i = $#hist; $i; --$i) {
  422.             last if $hist[$i] =~ $pat;
  423.             }
  424.             if (!$i) {
  425.             print OUT "No such command!\n\n";
  426.             next CMD;
  427.             }
  428.             $cmd = $hist[$i] . "\n";
  429.             print OUT $cmd;
  430.             redo CMD; };
  431.         $cmd =~ /^H\b\s*(-(\d+))?/ && do {
  432.             $end = $2?($#hist-$2):0;
  433.             $hist = 0 if $hist < 0;
  434.             for ($i=$#hist; $i>$end; $i--) {
  435.             print OUT "$i: ",$hist[$i],"\n"
  436.                 unless $hist[$i] =~ /^.?$/;
  437.             };
  438.             next CMD; };
  439.         $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
  440.         $cmd =~ /^=/ && do {
  441.             if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
  442.             $alias{$k}="s~$k~$v~";
  443.             print OUT "$k = $v\n";
  444.             } elsif ($cmd =~ /^=\s*$/) {
  445.             foreach $k (sort keys(%alias)) {
  446.                 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
  447.                 print OUT "$k = $v\n";
  448.                 } else {
  449.                 print OUT "$k\t$alias{$k}\n";
  450.                 };
  451.             };
  452.             };
  453.             next CMD; };
  454.         }
  455.         $evalarg = $cmd; &eval;
  456.         print OUT "\n";
  457.     }
  458.     if ($post) {
  459.         $evalarg = $post; &eval;
  460.     }
  461.     }
  462.     ($@, $!, $[, $,, $/, $\) = @saved;
  463. }
  464.  
  465. sub save {
  466.     @saved = ($@, $!, $[, $,, $/, $\);
  467.     $[ = 0; $, = ""; $/ = "\n"; $\ = "";
  468. }
  469.  
  470. # The following takes its argument via $evalarg to preserve current @_
  471.  
  472. sub eval {
  473.     eval "$usercontext $evalarg; &DB'save";
  474.     print OUT $@;
  475. }
  476.  
  477. sub action {
  478.     local($action) = @_;
  479.     while ($action =~ s/\\$//) {
  480.     print OUT "+ ";
  481.     $action .= &gets;
  482.     }
  483.     $action;
  484. }
  485.  
  486. sub gets {
  487.     local($.);
  488.     <IN>;
  489. }
  490.  
  491. sub catch {
  492.     $signal = 1;
  493. }
  494.  
  495. sub sub {
  496.     push(@stack, $single);
  497.     $single &= 1;
  498.     $single |= 4 if $#stack == $deep;
  499.     if (wantarray) {
  500.     @i = &$sub;
  501.     $single |= pop(@stack);
  502.     @i;
  503.     }
  504.     else {
  505.     $i = &$sub;
  506.     $single |= pop(@stack);
  507.     $i;
  508.     }
  509. }
  510.  
  511. $single = 1;            # so it stops on first executable statement
  512. @hist = ('?');
  513. $SIG{'INT'} = "DB'catch";
  514. $deep = 100;        # warning if stack gets this deep
  515. $window = 10;
  516. $preview = 3;
  517.  
  518. @stack = (0);
  519. @ARGS = @ARGV;
  520. for (@args) {
  521.     s/'/\\'/g;
  522.     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  523. }
  524.  
  525. if (-f '.perldb') {
  526.     do './.perldb';
  527. }
  528. elsif (-f "$ENV{'LOGDIR'}/.perldb") {
  529.     do "$ENV{'LOGDIR'}/.perldb";
  530. }
  531. elsif (-f "$ENV{'HOME'}/.perldb") {
  532.     do "$ENV{'HOME'}/.perldb";
  533. }
  534.  
  535. 1;
  536.